/**
 * 用户登录Servlet
 */
package org.morningangel.servlet.android;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import net.sf.json.JSONObject;

import org.morningangel.android.bean.ActivityBeanList;
import org.morningangel.bean.Activity;
import org.morningangel.bean.User;
import org.morningangel.dao.MainPushDao;
import org.morningangel.dao.UserDao;
import org.morningangel.dao.impl.MainPushDaoImpl;
import org.morningangel.dao.impl.UserDaoImpl;
import org.morningangel.util.EncryptUtil;

@WebServlet(name = "AndroidLoginServlet", urlPatterns = "/android/login")
public class LoginServlet extends HttpServlet {

	private static final long serialVersionUID = 1326704682342741097L;

	public void doGet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	public void doPost(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		request.setCharacterEncoding("utf-8");
		response.setCharacterEncoding("utf-8");
		response.setContentType("text/html;charset=utf-8");
		String account = request.getParameter("account");
		String password = request.getParameter("password");
		String loginMethod = request.getParameter("loginMethod");
		String userName = request.getParameter("userName");
		UserDao userDao = new UserDaoImpl();
		MainPushDao mainPushDao = new MainPushDaoImpl();
		ActivityBeanList result = null;
		List<Activity> activities =mainPushDao.getActivitys(1);
		User user = null;
		if (loginMethod.equals("1")) {
			//手机/邮箱登录
			user = userDao.login(account);
			if (user == null) {
				//账号不存在
				result = new ActivityBeanList();
			} else if (EncryptUtil.md5(password).equals(user.getPassword()))  {
				//登陆成功
				result = new ActivityBeanList(user, 1, activities);
			} else {
				//密码错误
				result = new ActivityBeanList();
			}
		} else if (loginMethod.equals("2")) {
			//微博登陆
			user = userDao.loginByWeibo(account);
			if (user == null) {
				//第一次用该微博号登陆
				user = new User();
				user.setWeibo(account);
				user.setUsername(userName);
				user.setRegisterTime(new Date());
				userDao.register(user);
				user = userDao.loginByWeibo(account);
			} 
			result = new ActivityBeanList(user, 1, activities);
		} else if (loginMethod.equals("3")) {
			//微博登陆
			user = userDao.loginByQQ(account);
			if (user == null) {
				//第一次用该QQ号登陆
				user = new User();
				user.setQq(account);
				user.setUsername(userName);
				user.setRegisterTime(new Date());
				userDao.register(user);
				user = userDao.loginByQQ(account);
			}
			result = new ActivityBeanList(user, 1, activities);
		}
		JSONObject jsonObject = JSONObject.fromObject(result);
		PrintWriter out = response.getWriter();
		out.print(jsonObject.toString());
		out.flush();
		out.close();
	}

}
